# meson configuration for gperf
#
# Copyright (C) 2020 Tim-Philipp Müller <tim centricular com>
#
# This file is part of GNU GPERF.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

texi2dvi = find_program('texi2dvi', required: get_option('doc-dvi'))
texi2pdf = find_program('texi2pdf', required: get_option('doc-pdf'))
makeinfo = find_program('makeinfo', required: get_option('doc-info'))
help2man = find_program('help2man', required: get_option('doc-man')) # from srcdir
texi2html = find_program('texi2html', required: get_option('doc-html')) # from srcdir
dvips = find_program('dvips', required: get_option('doc-ps'))

docdir = get_option('datadir') / 'doc' / 'gperf'
mandir = get_option('mandir') / 'man1'
infodir = get_option('infodir')

examplesdir = docdir / 'examples'

if makeinfo.found()
  custom_target('gperf.info',
    output: 'gperf.info',
    input: ['gperf.texi', 'gpl-3.0.texi'],
    command: [makeinfo, '--no-split', '-o', '@OUTPUT@', '@INPUT0@'],
    install_dir: infodir,
    install: true)
endif

gperf_pdf = []
if texi2pdf.found()
  gperf_pdf = custom_target('gperf.pdf',
    output: 'gperf.pdf',
    input: ['gperf.texi', 'gpl-3.0.texi'],
    command: [texi2pdf, '--quiet', '-o', '@OUTPUT@', '@INPUT0@'],
    install_dir: docdir,
    install: true)
endif

if texi2dvi.found()
  gperf_dvi = custom_target('gperf.dvi',
    output: 'gperf.dvi',
    input: ['gperf.texi', 'gpl-3.0.texi'],
    command: [texi2dvi, '--quiet', '-o', '@OUTPUT@', '@INPUT0@'],
    depends: gperf_pdf, # avoid parallel run with texi2pdf which uses same temp dirs
    install_dir: docdir,
    install: true)
endif

if dvips.found()
  if get_option('doc-ps').enabled() and not texi2dvi.found()
    error('doc-ps option was enabled, but required tool texi2dvi was not found')
  endif
  custom_target('gperf.ps',
    output: 'gperf.ps',
    depend_files: ['gperf.texi', 'gpl-3.0.texi'],
    command: [dvips, '-D600', '-q', '-o', '@OUTPUT@', gperf_dvi],
    install_dir: docdir,
    install: true)
endif

if help2man.found()
  # Can't use install_man() here because it doesn't accept a custom_target (yet)
  custom_target('gperf.1',
    output: 'gperf.1',
    command: [help2man, '--name=generate a perfect hash function from a key set', '--section=1', '-o', '@OUTPUT@', gperf_exe],
    install_dir: mandir,
    install: true)
endif

if texi2html.found()
  gperf_html = custom_target('gperf.html',
    output: 'gperf.html',
    input: ['gperf.texi', 'gpl-3.0.texi'],
    command: [texi2html, '-number', '-monolithic', '-outdir=@OUTDIR@', '@INPUT0@'],
    install_dir: docdir,
    install: true)
endif

# This list is based on what the debian package ships
if not get_option('doc-examples').disabled()
  install_data('../tests/c++.gperf', '../tests/c.gperf', '../tests/c-parse.gperf',
     install_dir: examplesdir)
endif
